package com.ailatrieuphu;

import org.andengine.engine.camera.Camera;
import org.andengine.engine.options.EngineOptions;
import org.andengine.engine.options.ScreenOrientation;
import org.andengine.engine.options.resolutionpolicy.FillResolutionPolicy;
import org.andengine.entity.scene.Scene;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.andengine.ui.activity.SimpleBaseGameActivity;

import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;

import com.ailatrieuphu.item.Player;
import com.ailatrieuphu.util.Config;

public abstract class MyActivity extends SimpleBaseGameActivity {
	public static MyActivity instance = null;
	public Camera mCamera;
	public Scene mScene;
	public long timeSplash = 800;
	public static int w, h;
	public static Connection connection = null;
	public static Player mPlayer=null;
	

	@Override
	protected void onCreate(Bundle pSavedInstanceState) {
		super.onCreate(pSavedInstanceState);
		instance = this;
		if(mPlayer==null){
			mPlayer=new Player();
		}
		Config.getDisplayScreen(this);
		w = Config.getSCREEN_WIDTH();
		h = Config.getSCREEN_HEIGHT();
	}

	@Override
	public EngineOptions onCreateEngineOptions() {
		Config.getDisplayScreen(this);
		mCamera = new Camera(0, 0, Config.SCREEN_WIDTH, Config.SCREEN_HEIGHT);
		EngineOptions engineOptions = new EngineOptions(true,
				ScreenOrientation.LANDSCAPE_FIXED, new FillResolutionPolicy(),
				mCamera);
		return engineOptions;
	}

	public void doConnect(){
		if (connection == null) {
			connection = new Connection();
			connection.execute();
		}
	}
	
	public abstract void loadResources();

	public abstract void loadScenes();

	public abstract void processMessage(Message message);
	
	public abstract void disConnection();
	@Override
	protected void onCreateResources() {
		// TODO Auto-generated method stub;
		BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
		loadResources();
	}

	@Override
	protected Scene onCreateScene() {
		// TODO Auto-generated method stub
		mScene = new Scene();
		loadScenes();
		return mScene;
	}

	public void startActivity(Class<?> activity) {
		this.startActivity(new Intent(this, activity));
	}

	public void showToast(final String text) {

		run(new IAction() {
			@Override
			public void perform() {
				Toast.makeText(instance, text, Toast.LENGTH_SHORT).show();
			}
		});
	}

	public void showToastLong(final String text) {
		run(new IAction() {
			@Override
			public void perform() {
				Toast.makeText(instance, text, Toast.LENGTH_LONG).show();
			}
		});
	}

	public final void run(final IAction action) {
		instance.runOnUiThread(new Runnable() {
			@Override
			public void run() {
				if (action != null)
					action.perform();
			}
		});
	}
	
}
